const Git = require("nodegit")
const thirdPartyRouter = require('koa-router')()

thirdPartyRouter.get('/git/log', async (ctx: any) => {
  const data = await getCommits()
  ctx.body = {
    code: 200,
    message: '成功',
    data
  }
})

async function getCommits () {
  const commitList:any = []
  const repo = await Git.Repository.open('../')
  const commit = await repo.getBranchCommit('master')
  const history = await commit.history();
  history.start()
  
  return new Promise(resolve => {
    history.on("commit", function(c: any) {
      const author = c.author()
      commitList.push({
        sha: c.sha(),
        author: author.name() + " <" + author.email() + ">",
        date: c.date(),
        message: c.message(),
        summary: c.summary(),
      })
      if (c.date().getTime() === 1665394031000) {
        resolve(commitList)
      }
    })
  })
}

module.exports = thirdPartyRouter
